#ifdef KITSCHY_DEBUG_MEMORY 
#include "debug_memorymanager.h"
#endif

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "math.h"

#include "BList.h"
#include "BVector.h"
#include "geometrics.h"
#include "quaternion.h"
#include "symbol.h"
#include "sort.h"
#include "ontology.h"
#include "featureterm.h"
#include "FTKBase.h"

#include "AWLocalViewport.h"

#include "AWEntity.h"
#include "AWMessage.h"
#include "AWScenario.h"
#include "AWBody.h"
#include "AWSensor.h"
#include "AWBrain.h"
#include "AWTracerBrain.h"
#include "AWFileTracerBrain.h"
#include "AWAgent.h"
#include "AWWorld.h"

#include "loader.h"



AW_FileTracerBrain::AW_FileTracerBrain(Ontology *O,FTKBase *Memory) : AW_TracerBrain(O,Memory) 
{
	optime=-1.0;
	tracename=0;
	trace=0;
	trace_pos=0;

	class_name->set("AW_FileTracerBrain");
} /* AW_FileTracerBrain::AW_TracerBrain */ 


AW_FileTracerBrain::AW_FileTracerBrain(char *tracefile,Ontology *O,FTKBase *Memory) : AW_TracerBrain(O,Memory) 
{
	optime=-1.0;
	tracename=new char[strlen(tracefile)+1];
	strcpy(tracename,tracefile);
	trace=0;
	trace_pos=0;
	trace=fopen(tracename,"r");

	class_name->set("AW_FileTracerBrain");
} /* AW_FileTracerBrain::AW_FileTracerBrain */ 


AW_FileTracerBrain::AW_FileTracerBrain(FILE *fp,Ontology *O,FTKBase *Memory) : AW_TracerBrain(O,Memory)
{
	optime=-1.0;
	
	load(fp);

	trace=0;
	trace_pos=0;
	trace=fopen(tracename,"r");
	fseek(trace,trace_pos,SEEK_SET);

	class_name->set("AW_FileTracerBrain");
} /* AW_FileTracerBrain::AW_TracerBrain */ 


AW_FileTracerBrain::~AW_FileTracerBrain() 
{
	delete []tracename;
	tracename=0;
	if (trace!=0) {
		fclose(trace);
		trace=0;
	} /* if */ 
} /* AW_FileTracerBrain::~AW_FileTracerBrain */ 



bool AW_FileTracerBrain::load(FILE *fp)
{
	char tmp[256];

	if (!AW_TracerBrain::load(fp)) return false;
	if (1!=fscanf(fp,"%f",&optime)) return false;
	if (1!=fscanf(fp,"%s",tmp)) return false;
	tracename=new char[strlen(tmp)+1];
	strcpy(tracename,tmp);
	if (1!=fscanf(fp,"%i",&trace_pos)) return false;

	return true;
} /* AW_FileTracerBrain::load */ 


bool AW_FileTracerBrain::save(FILE *fp)
{
	if (!AW_TracerBrain::save(fp)) return false;
	fprintf(fp,"%f\n",optime);
	fprintf(fp,"%s\n",tracename);
	fprintf(fp,"%i\n",trace_pos);
	
	return true;
} /* AW_FileTracerBrain::save */ 


void AW_FileTracerBrain::decode_input(float time,float timeinc,
									  Symbol *me,
									  List<FeatureTerm> *,
									  List<FeatureTerm> *,
									  List<FeatureTerm> *l)
{
	if (optime==-1.0 && trace!=0) {
		if (1!=fscanf(trace,"%f",&optime)) return;
	} /* if */ 

	if (time>=optime && trace!=0) {
		FeatureTerm *op;

		op=FeatureTerm::fromFileNOOS(trace,m_memory,m_ontology);
		l->Add(op);

		optime=-1.0;
	} /* if */ 

} /* AW_FileTracerBrain::decode_input */ 



bool AW_FileTracerBrain::is_a(char *c)
{
	if (strcmp(c,"AW_FileTracerBrain")==0) return true;

	return AW_TracerBrain::is_a(c);
} /* AW_FileTracerBrain::is_a */ 


bool AW_FileTracerBrain::is_a(Symbol *c)
{
	if (c->cmp("AW_FileTracerBrain")) return true;

	return AW_TracerBrain::is_a(c);
} /* AW_FileTracerBrain::is_a */ 
